//
//  TestUITabBarController.m
//  MKUITestApp
//
//  Created by 苏尚进 on 2023/8/3.
//  Copyright © 2023 moon. All rights reserved.
//

#import "TestUITabBarController.h"
#import "Menu.h"
#import "ListViewController.h"
#import "FunTableViewController.h"
#import "TestElementsTableViewController.h"
#import "TestElementsViewController.h"
#import "TestMKUICustomTabBarItem.h"
#import <objc/runtime.h>
@interface UITabBarControllerParamListViewController : ListViewController<UIScrollViewDelegate>
@property(nonatomic,strong) TestScrollViewScrollHandle *scrollHandle;
@end
@implementation UITabBarControllerParamListViewController
+ (MKTFuncItem *)funcItem{
    return [MKTFuncItem funcWithName:@"Param面板" iconImage:Menu.randomImage viewControllerClass:self];
}
- (id)init{
    if(self=[super init]){
        TestMKUICustomTabBarItem *tabBarItem = [[TestMKUICustomTabBarItem alloc] init];
        tabBarItem.badgeValue = @"1";
        [tabBarItem test_setValueWithFuncItem:[self.class funcItem]];
        tabBarItem.selectedImageOfScrollToTop = [[UIImage mk_disclosureIndicatorImageWithColor:UIColor.systemBlueColor] mk_rotateImageWithRadians:M_PI_2];
        tabBarItem.imageOfScrollToTop = [[UIImage mk_disclosureIndicatorImageWithColor:[UIColor colorWithWhite:0.57 alpha:1]] mk_rotateImageWithRadians:M_PI_2];
        self.mk_customTabBarItem = tabBarItem;
    }
    return self;
}
- (void)viewDidLoad{
    [super viewDidLoad];
    //监测滚动
    self.scrollHandle = [[TestScrollViewScrollHandle alloc] init];
    self.scrollHandle.position = 100;
    @MK_WEAKIFY(self);
    self.scrollHandle.whenOverPosition = ^(TestScrollViewScrollHandle *handle,BOOL isDown) {
        @MK_NORMALIZE(self);
        TestMKUICustomTabBarItem *tabBarItem = self.mk_customTabBarItem;
        TestMKUICustomTabBarItemCellViewScrollToTop *itemCell = tabBarItem.itemCell;
        [itemCell playScrollToTop:isDown animated:YES];
    };
    self.listView.delegate = self;
    self.listView.listFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 50)];
    [self __reloadData];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.scrollHandle scrollViewDidScroll:scrollView];
}
- (void)__reloadData{
    [self.listView deleteAllSections];
    [self.listView addSection];
    @MK_WEAKIFY(self);
    {
        MKUIElementListViewCellDetail *el = [[MKUIElementListViewCellDetail alloc] init];
        el.elementView.titleText = @"添加ViewController";
        el.elementView.accessView = el.elementView.disclosureIndicatorAccessView;
        el.whenClick = ^(__kindof MKUIListSelectionCellViewBase * _Nonnull el) {
            @MK_NORMALIZE(self);
            NSMutableArray<UIViewController *> *viewControllers = [self.customTabBarController.viewControllers mutableCopy];
            [viewControllers addObject:[UITabBarControllerParamListViewController new]];
            [self.customTabBarController setViewControllers:viewControllers animated:YES];
        };
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellDetail *el = [[MKUIElementListViewCellDetail alloc] init];
        el.elementView.titleText = @"删除ViewController";
        el.elementView.accessView = el.elementView.disclosureIndicatorAccessView;
        el.whenClick = ^(__kindof MKUIListSelectionCellViewBase * _Nonnull el) {
            @MK_NORMALIZE(self);
            NSMutableArray<UIViewController *> *viewControllers = [self.customTabBarController.viewControllers mutableCopy];
            [viewControllers removeLastObject];
            [self.customTabBarController setViewControllers:viewControllers animated:YES];
        };
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellDetail *el = [[MKUIElementListViewCellDetail alloc] init];
        el.elementView.titleText = @"viewControllers顺序对调";
        el.elementView.accessView = el.elementView.disclosureIndicatorAccessView;
        el.whenClick = ^(__kindof MKUIListSelectionCellViewBase * _Nonnull el) {
            @MK_NORMALIZE(self);
            NSMutableArray<UIViewController *> *viewControllers = [[NSMutableArray alloc] init];
            for(UIViewController *vc in self.customTabBarController.viewControllers.reverseObjectEnumerator){
                [viewControllers addObject:vc];
            }
            [self.customTabBarController setViewControllers:viewControllers animated:YES];
        };
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellDetail *el = [[MKUIElementListViewCellDetail alloc] init];
        el.elementView.titleText = @"viewControllers清空";
        el.elementView.accessView = el.elementView.disclosureIndicatorAccessView;
        el.whenClick = ^(__kindof MKUIListSelectionCellViewBase * _Nonnull el) {
            @MK_NORMALIZE(self);
            NSMutableArray<UIViewController *> *viewControllers = [[NSMutableArray alloc] init];
            [self.customTabBarController setViewControllers:viewControllers animated:YES];
        };
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSwitch *el = [[MKUIElementListViewCellSwitch alloc] init];
        el.elementView.titleText = @"自定义过场动画";
        el.elementView.on = self.customTabBarController.useTransition;
        el.elementView.whenChange = ^(MKUIElementViewSwitch * _Nonnull el, BOOL on) {
            @MK_NORMALIZE(self);
            self.customTabBarController.useTransition = on;
        };
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSwitch *el = [[MKUIElementListViewCellSwitch alloc] init];
        el.elementView.titleText = @"设置translucent";
        el.elementView.on = self.customTabBarController.customTabBar.translucent;
        el.elementView.whenChange = ^(MKUIElementViewSwitch * _Nonnull el, BOOL on) {
            @MK_NORMALIZE(self);
            self.customTabBarController.customTabBar.translucent = on;
        };
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellDetail *el = [[MKUIElementListViewCellDetail alloc] init];
        el.elementView.titleText = @"设置tabBarItemHeight";
        el.elementView.detailText = @(self.customTabBarController.customTabBar.itemHeight).stringValue;
        el.elementView.accessView = el.elementView.disclosureIndicatorAccessView;
        el.whenClick = ^(__kindof MKUIElementListViewCellDetail * _Nonnull el) {
            @MK_NORMALIZE(self);
            self.customTabBarController.customTabBar.itemHeight = self.customTabBarController.customTabBar.itemHeight==48?68:48;
            el.elementView.detailText = @(self.customTabBarController.customTabBar.itemHeight).stringValue;
        };
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSwitch *el = [[MKUIElementListViewCellSwitch alloc] init];
        el.elementView.titleText = @"设置背景图片";
        el.elementView.on = self.customTabBarController.customTabBar.backgroundImage!=nil;
        el.elementView.whenChange = ^(MKUIElementViewSwitch * _Nonnull el, BOOL on) {
            @MK_NORMALIZE(self);
            self.customTabBarController.customTabBar.backgroundImage = on?[UIImage mk_imageWithUIColor:UIColor.redColor size:CGSizeMake(1, 20)]:nil;
        };
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSwitch *el = [[MKUIElementListViewCellSwitch alloc] init];
        el.elementView.titleText = @"设置tabBar.hidden";
        el.elementView.on = self.customTabBarController.tabBar.hidden;
        el.elementView.whenChange = ^(MKUIElementViewSwitch * _Nonnull el, BOOL on) {
            @MK_NORMALIZE(self);
            self.customTabBarController.tabBar.hidden = on;
        };
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSwitch *el = [[MKUIElementListViewCellSwitch alloc] init];
        el.elementView.titleText = @"显示badgeValue";
        el.elementView.on = self.mk_customTabBarItem.badgeValue.length!=0;
        el.elementView.whenChange = ^(MKUIElementViewSwitch * _Nonnull el, BOOL on) {
            @MK_NORMALIZE(self);
            self.mk_customTabBarItem.badgeValue = on?@"+99999":nil;
        };
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellDetail *el = [[MKUIElementListViewCellDetail alloc] init];
        el.elementView.titleText = @"badgeValue++";
        el.elementView.accessView = el.elementView.disclosureIndicatorAccessView;
        el.whenClick = ^(__kindof MKUIListSelectionCellViewBase * _Nonnull el) {
            @MK_NORMALIZE(self);
            if(self.mk_customTabBarItem.badgeValue.length==0) self.mk_customTabBarItem.badgeValue = @"1";
            else self.mk_customTabBarItem.badgeValue = @(self.mk_customTabBarItem.badgeValue.integerValue+1).stringValue;
            self.tabBarController.selectedIndex++;
        };
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellDetail *el = [[MKUIElementListViewCellDetail alloc] init];
        el.elementView.titleText = @"延时badgeValue++";
        el.elementView.accessView = el.elementView.disclosureIndicatorAccessView;
        el.whenClick = ^(__kindof MKUIListSelectionCellViewBase * _Nonnull el) {
            @MK_NORMALIZE(self);
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                if(self.mk_customTabBarItem.badgeValue.length==0) self.mk_customTabBarItem.badgeValue = @"1";
                else self.mk_customTabBarItem.badgeValue = @(self.mk_customTabBarItem.badgeValue.integerValue+1).stringValue;
                
                self.tabBarController.viewControllers[3].mk_customTabBarItem.badgeValue = self.mk_customTabBarItem.badgeValue;
                
                if(self.mk_customTabBarItem.badgeValue.length==0) self.mk_customTabBarItem.badgeValue = @"1";
                else self.mk_customTabBarItem.badgeValue = @(self.mk_customTabBarItem.badgeValue.integerValue+1).stringValue;
            });
        };
        [self.listView addItem:el];
    }
    
    [self.listView addSection];
    {
        MKUIElementListViewCellDetail *el = [[MKUIElementListViewCellDetail alloc] init];
        el.elementView.titleText = @"self.view.safeAreaInsets";
        if (@available(iOS 11.0, *)) {
            el.elementView.subTitleText = NSStringFromUIEdgeInsets(self.view.safeAreaInsets);
        }
        el.elementView.accessView = el.elementView.disclosureIndicatorAccessView;
        el.whenClick = ^(__kindof MKUIElementListViewCellDetail * _Nonnull el) {
            @MK_NORMALIZE(self);
            if (@available(iOS 11.0, *)) {
                el.elementView.subTitleText = NSStringFromUIEdgeInsets(self.view.safeAreaInsets);
            }
        };
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellDetail *el = [[MKUIElementListViewCellDetail alloc] init];
        el.elementView.titleText = @"打开新页面";
        el.elementView.subTitleText = @"使用self.navigationController打开，应该可以打开";
        el.elementView.accessView = el.elementView.disclosureIndicatorAccessView;
        el.whenClick = ^(__kindof MKUIListSelectionCellViewBase * _Nonnull el) {
            @MK_NORMALIZE(self);
            UIViewController *vc = [[UITabBarControllerParamListViewController alloc] init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                if(self.tabBarItem.badgeValue.length==0) self.tabBarItem.badgeValue = @"1";
                else self.tabBarItem.badgeValue = @(self.tabBarItem.badgeValue.integerValue+1).stringValue;
            });
        };
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellDetail *el = [[MKUIElementListViewCellDetail alloc] init];
        el.elementView.titleText = @"延迟打开新页面";
        el.elementView.subTitleText = @"延迟5秒打开新页面";
        el.elementView.accessView = el.elementView.disclosureIndicatorAccessView;
        el.whenClick = ^(__kindof MKUIListSelectionCellViewBase * _Nonnull el) {
            @MK_NORMALIZE(self);
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                UIViewController *vc = [[UITabBarControllerParamListViewController alloc] init];
                vc.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:vc animated:YES];
            });
        };
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellDetail *el = [[MKUIElementListViewCellDetail alloc] init];
        el.elementView.titleText = @"打开新页面";
        el.elementView.subTitleText = @"使用self.mk_navigationControllerOfFirst打开，应该可以打开";
        el.elementView.accessView = el.elementView.disclosureIndicatorAccessView;
        el.whenClick = ^(__kindof MKUIListSelectionCellViewBase * _Nonnull el) {
            @MK_NORMALIZE(self);
            UIViewController *vc = [[UITabBarControllerParamListViewController alloc] init];
            [self.mk_navigationControllerOfFirst pushViewController:vc animated:YES];
        };
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellDetail *el = [[MKUIElementListViewCellDetail alloc] init];
        el.elementView.titleText = @"选中下一个页面";
        el.elementView.accessView = el.elementView.disclosureIndicatorAccessView;
        el.whenClick = ^(__kindof MKUIListSelectionCellViewBase * _Nonnull el) {
            @MK_NORMALIZE(self);
            self.tabBarController.selectedIndex++;
        };
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellDetail *el = [[MKUIElementListViewCellDetail alloc] init];
        el.elementView.titleText = @"测试退出";
        el.elementView.subTitleText = @"退出nav之后，还修改vc.mk_customTabBarItem.badgeValue";
        el.elementView.accessView = el.elementView.disclosureIndicatorAccessView;
        el.whenClick = ^(__kindof MKUIListSelectionCellViewBase * _Nonnull el) {
            @MK_NORMALIZE(self);
            UIViewController *vc = self;
            [self.navigationController popViewControllerAnimated:YES];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                vc.mk_customTabBarItem.badgeValue = @"....";
            });
        };
        [self.listView addItem:el];
    }
    
    [self.listView reloadData];
}
- (TestUITabBarController *)customTabBarController{
    if(![self.tabBarController isKindOfClass:TestUITabBarController.class])return nil;
    return (TestUITabBarController *)self.tabBarController;
}
@end

@interface TestUITabBarController ()<MKUItemFlowCollectionViewDelegate>
@property(nonatomic,strong) UIViewController *blankViewController;
@end

@implementation TestUITabBarController
+ (MKTFuncItem *)funcItem{
    return [MKTFuncItem funcWithName:@"系统UITabBarController属性测试" iconImage:Menu.randomImage viewControllerClass:self];
}
- (id)init{
    if(self=[super init]){
        self.tabBarItem.title = [self.class funcItem].name;
        self.tabBarItem.badgeValue = @"Hot";
        self.title = [self.class funcItem].name;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.animationControllerTransition = [[UITabBarControllerTestAnimatedTransitioning alloc] init];
    //
    self.customTabBar.collectionView.clipsToBounds = NO;
    NSMutableArray<UIViewController *> *viewControllers = [[NSMutableArray alloc] init];
    {
        UIViewController *vc = [[UITabBarControllerParamListViewController alloc] init];
        vc.mk_customTabBarItem.badgeValue = @"1";
        [viewControllers addObject:vc];
    }
    {
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[UITabBarControllerParamListViewController new]];
        nav.mk_customTabBarItem.title = @"嵌套Nav";
        nav.mk_customTabBarItem.badgeValue = @"1";
        nav.mk_customTabBarItem.image = Menu.randomImage;
        nav.mk_customTabBarItem.badgeStyle = MKUICustomTabBarItemBadgeStyleDot;
        [viewControllers addObject:nav];
    }
    {
        UIViewController *vc = [[UIViewController alloc] init];
        vc.mk_customTabBarItem.itemCellClass = MKUICustomTabBarItemCellViewTestBigIcon.class;
        vc.mk_customTabBarItem.image = Menu.randomImage;
        [viewControllers addObject:vc];
        self.blankViewController = vc;
    }
    {
        UIViewController *vc = [[FunTableViewController alloc] init];
        vc.mk_customTabBarItem.itemCellClass = MKUICustomTabBarItemCellViewTestBigIcon.class;
        vc.mk_customTabBarItem.image = Menu.randomImage;
        [viewControllers addObject:vc];
    }
    {
        UIViewController *vc = [[UIViewController alloc] init];
        vc.view.backgroundColor = UIColor.redColor;
        [viewControllers addObject:vc];
    }
    self.viewControllers = viewControllers;
    self.selectedIndex = 1;
    self.delegate = self;
    [self _configNavigationForViewController:self.selectedViewController];
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    if(viewController==self.blankViewController){//点击了特殊的空白名称，此时不进行选中，弹一个提示窗
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"点击空白视图" message:nil preferredStyle:(UIAlertControllerStyleAlert)];
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
        }]];
        [self presentViewController:alert animated:YES completion:nil];
        return NO;
    }
    if(tabBarController.selectedViewController==viewController){//再次点击选中的vc
        NSArray<UIScrollView *> *scrollViews = [viewController.view mk_subviewsWithClass:UIScrollView.class resursion:YES];
        for(UIScrollView *scrollView in scrollViews){
            [scrollView mk_scrollToTopWithAnimated:YES];
        }
    }
    return YES;
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    [self _configNavigationForViewController:viewController];
}
- (void)_configNavigationForViewController:(UIViewController *)viewController{
    self.title = viewController.title;
    self.navigationItem.rightBarButtonItems = viewController.navigationItem.rightBarButtonItems;
    self.navigationItem.leftBarButtonItems = viewController.navigationItem.leftBarButtonItems;
}
- (nullable id <UIViewControllerInteractiveTransitioning>)tabBarController:(UITabBarController *)tabBarController interactionControllerForAnimationController:(id <UIViewControllerAnimatedTransitioning>)animationController API_AVAILABLE(ios(7.0)){
    return self.useTransition?self.animationControllerTransition:nil;
}

- (nullable id <UIViewControllerAnimatedTransitioning>)tabBarController:(UITabBarController *)tabBarController animationControllerForTransitionFromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC  API_AVAILABLE(ios(7.0)){
    return self.useTransition?self.animationControllerTransition:nil;
}
- (UITabBar *)tabBar{
    return [super tabBar];
}
@end
